<?
include "Facture.php";
include "Stats.php";
class MainController extends Controller {
	public $js = array("ventes",
		"jQuery/plugins/jqPlot/jquery.jqplot.min",
		"jQuery/plugins/jqPlot/jqplot.canvasAxisLabelRenderer.min",
	
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasTextRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.canvasAxisTickRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.categoryAxisRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.barRenderer.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.highlighter.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.cursor.min",
		"jQuery/plugins/jqPlot/plugins/jqplot.dateAxisRenderer.min"
	);
	
	public $css = array("ventes","jQuery/jqPlot/jquery.jqplot.min");
    public function dashboardAction(){
        $connect = $this->connections;
		$mysql = $connect->getConnection();
		$user_id = $_SESSION["user_id"];
		
		$query = "SELECT * FROM facture WHERE facture._id NOT IN ("
			    ."select facture._id FROM paiement_client INNER JOIN facture ON facture._id = paiement_client._facture "
			    ."WHERE user_id = '$user_id');";
		$res = $mysql->query($query);
		$waiting_paiements = 0;
		while($line = $res->fetch_object()){
			$factures[] = $line;
			$waiting_paiements += $line->montant_ht;
		}
    	$query = "SELECT * FROM facture_fournisseur WHERE facture_fournisseur._id NOT IN ("
			    ."select facture_fournisseur._id FROM paiement_fournisseur INNER JOIN facture_fournisseur ON facture_fournisseur._id = paiement_fournisseur._facture "
			    ."WHERE user_id = '$user_id');";
		$res = $mysql->query($query);
		$waiting_charges = 0;
		while($line = $res->fetch_object()){
			$factures_f[] = $line;
			$waiting_charges += $line->montant_ttc;
		}
        $aContacts = $connect->fetchAllModels("Contacts",array("user_id"=>$_SESSION["user_id"]));
        $aFournisseurs = $connect->fetchAllModels("Fournisseur",array("user_id"=>$_SESSION["user_id"]));
        
    	foreach($paiements as $paiement){
			$aPaiementsClient[$paiement->_facture] = $factures[$paiement->_facture];
		}
		
		$mysql = $connect->getConnection();
		$stats = new Stats($mysql);
		list($produits,$charges) = $stats->balance();
		$ca_tot = $stats->getCA("%");
		$achats_tot = $stats->getAchats("%");
		$ca_year = $stats->getCA(date("Y"));
		$achats_year = $stats->getAchats(date("Y"));
		$ca_month = $stats->getCA(date("Y"),date("m"));
		$achats_month = $stats->getAchats(date("Y"),date("m"));
		
		
        $vars = array(
			"factures"=>$factures,
        	"factures_f"=>$factures_f,
        	"waiting_paiements"=>$waiting_paiements,
        	"waiting_charges"=>$waiting_charges,
        	"aContacts"=>$aContacts,
        	"aFournisseurs"=>$aFournisseurs,
			"aPaiementsClient"=>$aPaiementsClient,
        	"produits"=>$produits,
        	"charges"=>$charges,
        	"ca_year"=>$ca_year,
        	"achats_year"=>$achats_year,
        	"ca_month"=>$ca_month,
        	"achats_month"=>$achats_month,
        	"ca_tot"=>$ca_tot,
        	"achats_tot"=>$achats_tot
        
		);
        
        $response = $this->render("dashboard",$vars);
        return $response;
    }
    public function disconnectAction(){
    	session_destroy();
    	header("location:/");
    }
}
?>